import datetime
from typing import List, Optional
from pydantic import BaseModel, Field, model_validator
from app.utils.tool import str_to_list, str_to_set


class RouteSchema(BaseModel):
    update_time: datetime.datetime = Field(description="更新时间")
    path: str = Field(description="路径")
    summary: str | None = Field(description="摘要")
    tags: List[str] = Field(description="标签")
    description: str = Field(description="描述")
    methods: set = Field(description="权限")
    endpoint: str = Field(description="端点函数")
    permission: str = Field(description="权限")

    class Config:
        from_attributes = True


class RouteInSchema(BaseModel):
    update_time: datetime.datetime | None = Field(description="更新时间")
    path: str | None = Field(description="路径")
    summary: str | None = Field(description="摘要")
    tags: str | None = Field(description="标签")
    description: str | None = Field(description="描述")
    methods: str | None = Field(description="权限")
    endpoint: str | None = Field(description="端点函数")
    permission: str = Field(description="权限")

    class Config:
        from_attributes = True

    # @model_validator(mode="before")
    # @classmethod
    # def validate_route(cls, values):
    #     if isinstance(values.get("tags"), List):
    #         values.update({"tags", list_to_str(values.get("tags"))})
    #     if isinstance(values.methods, set):
    #         values.update({"methods", set_to_str(values.get("methods"))})
    #     return values


class RouteOutSchema(RouteSchema):
    id: int = Field(description="自增ID")
    update_time: datetime.datetime | None = Field(description="更新时间")
    path: str | None = Field(description="路径")
    summary: str | None = Field(description="摘要")
    tags: str | None = Field(description="标签")
    description: str | None = Field(description="描述")
    methods: str | None = Field(description="权限")
    endpoint: str | None = Field(description="端点函数")
    permission: str| None = Field(description="权限")

    # @model_validator(mode="before")
    # def validate_route(cls, values):
    #     print("values type", type(values))
    #     try:
    #         if isinstance(values["tags"], str):
    #             values.replace(tags=str_to_list(values["tags"]))
    #             print("tags-------------", type(str_to_list(values["tags"])))
    #             # values.replace(tags=["tags"])
    #         if isinstance(values["methods"], str):
    #             values.replace(methods=str_to_set(values["methods"]))
    #     except Exception as e:
    #         print(e)
    #     return values
